package org.itheimma.springbootquickstart.controller;

import org.apache.ibatis.annotations.Param;
import org.itheimma.springbootquickstart.pojo.Images;
import org.itheimma.springbootquickstart.pojo.Users;
import org.itheimma.springbootquickstart.service.ipml.UserService;
import org.mockito.internal.matchers.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

@RestController
@CrossOrigin(origins = "*")
public class UserController {
    @Autowired
    ImgController imgController;
    @Autowired
    ThreeDModelController threeDModelController;
    @Autowired
    UserService userService;

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public String upload(HttpServletRequest request,
                         @RequestParam(value = "model", required = false) MultipartFile model,
                         @RequestParam(value = "images", required = false) MultipartFile[] images,
                         @RequestParam(value = "imgFileNames", required = false) String[] imgFileNames
    ) throws IOException {
        // 检查上传的模型是否为空
        if (model == null || request.getParameter("modelFileName") == ""){
            System.out.println(11);
            return "模型文件不允许为空！";
        }
        else {
            try {
                System.out.println(22);
                // 从请求中获取 FormData 数据,并处理
                //模型的url
                String Three_url1 = "/Users/biwei/Desktop/Spring_boot_start/springboot-quickstart/src/main/resources/static/models/" + request.getParameter("modelFileName");
                String Three_url2 = ResourceUtils.getURL("classpath:").getPath() + "static/models/" + request.getParameter("modelFileName");
                //上传者信息
                String uploaded_by = "biwei";
                //上传模型信息到数据库
                threeDModelController.upload(Three_url1, uploaded_by);
                //上传到本地（之后迁移云服务器）
//            Files.copy(model.getInputStream(), Path.of(Three_url1));
//            Files.copy(model.getInputStream(), Path.of(Three_url2));
                int three_id = userService.findThreeId();
                //图片允许为空
                if (images != null && images.length > 0) {
                    //上传图片信息到数据库
                    for (int i = 0; i < images.length; i++) {
                        String image_url1 = "/Users/biwei/Desktop/Spring_boot_start/springboot-quickstart/src/main/resources/static/images/" + imgFileNames[i];
                        String image_url2 = ResourceUtils.getURL("classpath:").getPath() + "static/images/" + imgFileNames[i];
                        imgController.upload(three_id, image_url1, uploaded_by);
                        //上传到本地（之后迁移云服务器）
//                Files.copy(images[i].getInputStream(), Path.of(image_url1));
//                Files.copy(images[i].getInputStream(), Path.of(image_url2));
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                return "文件上传失败";
            }
        }
        // 返回响应
        return "文件上传成功!";
    }
}
